package me.zhenqu.redis.utils;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-14
 * Time: 下午4:20
 * To change this template use File | Settings | File Templates.
 */
public class NumberCoder extends CacheCoder {
    public static final CacheCoder INST = new NumberCoder();

    @Override
    public byte[] encode(Object o) {
        if (o == null) return new byte[0];
        return o.toString().getBytes();
    }

    @Override
    public Object decode(byte[] val, int offset, int size) {
        if (size == 0) return null;
        String str = new String(val);
        if (str.indexOf('.') == -1) return Long.parseLong(str.substring(offset, offset + size));
        return Double.parseDouble(str);
    }
}
